package com.hand.wfive.view;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import com.hand.wfive.BuildConfig;
import com.hand.wfive.R;
import com.hand.wfive.activity.PrivacyActivity;
import com.qmuiteam.qmui.alpha.QMUIAlphaImageButton;

public class PrivacyDialog extends Dialog {

    /**
     * 显示的消息
     */
    private TextView messageTv;

    /**
     * 确认和取消按钮
     */
    private QMUIAlphaImageButton positiveBn;
    private TextView negtiveBn;

    /**
     * 设置确定取消按钮的回调
     */
    private final OnClickBottomListener onClickBottomListener;

    public PrivacyDialog(Context context, OnClickBottomListener onClickBottomListener) {
        super(context, R.style.CustomDialog);
        this.onClickBottomListener = onClickBottomListener;
    }

    public static boolean showPrivacy(Context context, OnClickBottomListener onClickBottomListener) {
        SharedPreferences sp = context.getSharedPreferences("LuckyPrivacy", Context.MODE_PRIVATE);
        String version = sp.getString("version", "1.0");
        boolean isFirst = sp.getBoolean("first", true);
        boolean isVersionFirst = !version.equals(BuildConfig.VERSION_NAME) || isFirst;
        if (isVersionFirst) {
            new PrivacyDialog(context, onClickBottomListener).show();
        }
        return isVersionFirst;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_privacy);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        //初始化界面控件
        initView();
        //初始化界面控件的事件
        initEvent();
    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后，向外界提供监听
        positiveBn.setOnClickListener(v -> {
            SharedPreferences sp = getContext().getSharedPreferences("LuckyPrivacy", Context.MODE_PRIVATE);
            sp.edit().putBoolean("first", false)
                    .putString("version", BuildConfig.VERSION_NAME)
                    .apply();
            onClickBottomListener.onPositiveClick(this);
            dismiss();
        });
        //设置取消按钮被点击后，向外界提供监听
        negtiveBn.setOnClickListener(v -> {
            onClickBottomListener.onNegtiveClick();
            dismiss();
        });
    }

    /**
     * 初始化界面控件的显示数据
     */
    private void refreshView() {
        final SpannableStringBuilder style = new SpannableStringBuilder();
        //设置文字
        style.append("(1)《隐私政策》中我们如何收集和使用您的个人信息。\n" +
                "(2)《隐私政策》中我们如何共享、转让、公开披露您的个人信息。\n" +
                "用户协议和隐私政策说明：\n阅读完整的《用户协议》和《隐私政策》了解详情信息内容。");

        //设置部分文字点击事件
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                PrivacyActivity.showRule(getContext(), PrivacyActivity.ShowUserAgreement);
            }
        };
        ClickableSpan clickableSpan1 = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                PrivacyActivity.showRule(getContext(), PrivacyActivity.ShowPolicy);
            }
        };
        style.setSpan(clickableSpan, 78, 82, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(clickableSpan1, 85, 89, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        messageTv.setText(style);

        //设置部分文字颜色
        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#31A16A"));
        ForegroundColorSpan foregroundColorSpan1 = new ForegroundColorSpan(Color.parseColor("#31A16A"));
        style.setSpan(foregroundColorSpan1, 78, 82, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(foregroundColorSpan, 85, 89, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        //配置给TextView
        messageTv.setMovementMethod(LinkMovementMethod.getInstance());
        messageTv.setText(style);
    }

    @Override
    public void show() {
        super.show();
        refreshView();
    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        negtiveBn = findViewById(R.id.negtive);
        positiveBn = findViewById(R.id.positive);
        messageTv = findViewById(R.id.message);
    }

    public interface OnClickBottomListener {
        /**
         * 点击确定按钮事件
         */
        void onPositiveClick(PrivacyDialog dialog);

        /**
         * 点击取消按钮事件
         */
        void onNegtiveClick();
    }

}

